﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace Autenticazione
{
    public partial class Coaches : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["EMail"] == null)
                Response.Redirect("Autenticazione.aspx");

            msgLoggedUser.Text = string.Format("Utente corrente: {0}", Session["EMail"].ToString());

            if (!IsPostBack)
                LoadGrid();
        }

        private void LoadGrid()
        {
            BusinessObject.Coach c = new BusinessObject.Coach();
            DataTable dt = null;
            DataTable dtView = null;

            if (Session["EMail"] == null)
                return;

            dt = c.GetTbCoaches(Session["Email"].ToString());

            dtView = dt.Clone();

            for (int i = dtgCoach.CurrentPageIndex * dtgCoach.PageSize; i <= (dtgCoach.CurrentPageIndex + 1) * dtgCoach.PageSize - 1; i++)
            {
                if (i <= dt.Rows.Count - 1)
                    dtView.ImportRow(dt.Rows[i]);
            }

            dtgCoach.VirtualItemCount = dt.Rows.Count; ;

            dtgCoach.DataSource = dtView;
            dtgCoach.DataBind();
        }

        protected void dtgCoach_Edit(object sender, DataGridCommandEventArgs e)
        {
            dtgCoach.EditItemIndex = e.Item.ItemIndex;

            LoadGrid();
        }

        protected void dtgCoach_Delete(object sender, DataGridCommandEventArgs e)
        {
            BusinessObject.Coach c = new BusinessObject.Coach();

            c.Line = long.Parse(dtgCoach.DataKeys[e.Item.ItemIndex].ToString());

            c.DeleteCoach();

            dtgCoach.EditItemIndex = -1;

            LoadGrid();
        }

        protected void dtgCoach_Cancel(object sender, DataGridCommandEventArgs e)
        {
            dtgCoach.EditItemIndex = -1;

            LoadGrid();
        }

        protected void dtgCoach_Update(object sender, DataGridCommandEventArgs e)
        {
            BusinessObject.Coach c = new BusinessObject.Coach();

            c.Line = long.Parse(dtgCoach.DataKeys[e.Item.ItemIndex].ToString());
            TextBox allenatore = e.Item.FindControl("txtAllenatore") as TextBox;
            TextBox telefono = e.Item.FindControl("txtPhone") as TextBox;

            if (allenatore != null)
                c.FullName = allenatore.Text.Replace("'", "''");

            if (telefono != null)
                c.Phone = telefono.Text;

            c.Update();

            dtgCoach.EditItemIndex = -1;
            LoadGrid();
        }

        protected void dtgCoach_SelectedIndexChanged(object sender, DataGridPageChangedEventArgs e)
        {
            dtgCoach.CurrentPageIndex = e.NewPageIndex;
            dtgCoach.EditItemIndex = -1;
            LoadGrid();
        }
    }
}